/**
 * 板块管理
 */
var pageCurr;
var form;
$(function() {
    layui.use(['table','util'], function(){
        var table = layui.table;
        var util = layui.util;
        form = layui.form;

        tableIns=table.render({
            elem: '#plateList',
            url:'/back/plate/plateList',
            method: 'post', //默认：get请求
            cellMinWidth: 80,
            page: true,
            request: {
                pageName: 'pageNum', //页码的参数名称，默认：pageNum
                limitName: 'pageSize' //每页数据量的参数名，默认：pageSize
            },
            response:{
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 200, //成功的状态码，默认：0
                countName: 'totals', //数据总数的字段名称，默认：count
                dataName: 'list' //数据列表的字段名称，默认：data
            },
            cols: [[
                {type:'checkbox'}
                ,{field:'plateName', title:'板块标题',align:'center'}
                ,{field:'plateUrl', title:'板块路径',align:'center'}
                ,{field:'articleCount', title:'板块博文数',align:'center'}
                ,{field:'description', title:'板块描述',align:'center'}
                ,{field: 'plateImg',
                    title: '板块图片缩略图',
                    width: 100,
                    align: 'center',
                    templet: function(d) {
                        var url = d.plateImg;
                        return '<a href="' + url + '" target="_blank " title="点击查看">' +
                            '<img src="' + url + '" style="height:40px;" />' +
                            '</a>';
                    }
                }
                ,{
                    field: 'createDate', title: '创建时间', templet: function (d) {
                        var createDate = util.toDateString(new Date(d.createDate).getTime(), "yyyy-MM-dd");
                        return '<div>' + createDate + '</div>'
                    }, align: 'center'
                }
                ,{
                    field: 'updateDate', title: '修改时间', templet: function (d) {
                        var updateDate = util.toDateString(new Date(d.updateDate).getTime(), "yyyy-MM-dd");
                        return '<div>' + updateDate + '</div>'
                    }, align: 'center'
                }
                ,{field:'plateStatus', title: '是否可用',align:'center'}
                ,{title:'操作',align:'center', toolbar:'#optBar'}
            ]],
            done: function(res, curr, count){
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                //console.log(res);
                //得到当前页码
                console.log(curr);
                $("[data-field='plateStatus']").children().each(function(){
                    if($(this).text()=='1'){
                        $(this).text("可用")
                    }else if($(this).text()=='0'){
                        $(this).text("禁用")
                    }
                });
                //得到数据总量
                //console.log(count);
                pageCurr=curr;
            }
        });

        //监听工具条
        table.on('tool(plateTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                //删除
                delPlate(data,data.id,data.plateName);
            } else if(obj.event === 'edit'){
                //编辑
                openPlate(data,"编辑");
            }else if(obj.event === 'recover'){
                //恢复
                recoverPlate(data,data.id);
            }
        });

        //监听提交
        form.on('submit(plateSubmit)', function(data){
            // TODO 校验
            formSubmit(data);
            return false;
        });
    });

    /*//搜索框
    layui.use(['form','laydate'], function(){
        var form = layui.form ,layer = layui.layer
            ,laydate = layui.laydate;
        //日期
        laydate.render({
            elem: '#startTime'
        });
        laydate.render({
            elem: '#endTime'
        });
        //TODO 数据校验
        //监听搜索框
        form.on('submit(searchSubmit)', function(data){
            //重新加载table
            load(data);
            return false;
        });
    });*/
});

//提交表单
function formSubmit(obj){
    $.ajax({
        type: "POST",
        data: $("#plateForm").serialize(),
        url: "/back/plate/setPlate",
        success: function (data) {
            if (data.code == 1) {
                layer.alert(data.msg,function(){
                    layer.closeAll();
                    load(obj);
                });
            } else {
                layer.alert(data.msg);
            }
        },
        error: function () {
            layer.alert("操作请求错误，请您稍后再试",function(){
                layer.closeAll();
                //加载load方法
                load(obj);//自定义
            });
        }
    });
}

//开通用户
function addPlate(){
    openPlate(null,"创建板块");
}
function openPlate(data,title){
    var plateStatus = null;
    var id = null;
    if(data != null){
        id = data.id;
    }
    /*else{
        $("#id").val(data.id);
        $("#plateName").val(data.plateName);
        $("#description").val(data.description);
        $("#plateImg").val(data.plateImg);
        plateStatus = data.plateStatus;
    }*/
    var pageNum = $(".layui-laypage-skip").find("input").val();
    $("#pageNum").val(pageNum);

    layer.open({
        type: 2,
        closeBtn: 1,
        title: '修改标签',
        area: ['900px','600px'],
        content: '/back/plate_operation?id=' + id,
        end:function(){

        }
    });
}

function delPlate(obj,id,name) {
    if(null!=id){
        layer.confirm('您确定要禁用'+name+'板块？', {
            btn: ['确认','返回'] //按钮
        }, function(){
            $.post("/back/plate/updatePlateStatus",{"id":id,"status":0},function(data){
                if (data.code == 1) {
                    layer.alert(data.msg,function(){
                        layer.closeAll();
                        load(obj);
                    });
                } else {
                    layer.alert(data.msg);
                }
            });
        }, function(){
            layer.closeAll();
        });
    }
}
//恢复
function recoverPlate(obj,id) {
    if(null!=id){
        layer.confirm('您确定要恢复吗？', {
            btn: ['确认','返回'] //按钮
        }, function(){
            $.post("/back/plate/updatePlateStatus",{"id":id,"status":1},function(data){
                if (data.code == 1) {
                    layer.alert(data.msg,function(){
                        layer.closeAll();
                        load(obj);
                    });
                } else {
                    layer.alert(data.msg);
                }
            });
        }, function(){
            layer.closeAll();
        });
    }
}

function load(obj){
    //重新加载table
    tableIns.reload({
        where: obj.field
        , page: {
            curr: pageCurr //从当前页码开始
        }
    });
}

function cleanPlate(){
    $("#PlateName").val("");
    $("#realName").val("");
    $("#password").val("");
    $('#roleId').html("");
}
